Linux环境下编译TensorFlow C++ API和测试方法总结(完美版) 您所在的位置:网站首页 tensorflow 编译 whl Linux环境下编译TensorFlow C++ API和测试方法总结(完美版)

Linux环境下编译TensorFlow C++ API和测试方法总结(完美版)

2023-11-04 14:44| 来源: 网络整理| 查看: 265

目录

前言

1、软件下载

2、不同平台版本对应要求

3、环境配置

4、配置TensorFlow安装选项

5、使用bazel进行编译生成动态库

6、编译其他依赖

7、测试一:简单测试编译好的TensorFlow C++ API

8、测试二:Tensorflow C++ API调用Python预训练模型

前言

TensorFlow C++ API的编译是线上使用TensorFlow C++ API调用预先训练好的模型完成预测项目的必经之路,但是TensorFlow C++ API的编译并不是这么顺利,期间遇到了很多错误,它对版本要求非常严格,版本不对应会出现很多问题,尤其protobuf是最重要的一个。本篇是记录TensorFlow C++ API顺利编译和测试的过程,为线上使用TensorFlow C++ API的使用打下基础。

1、软件下载

(1)tensorflow-1.13.1源码(tensorflow 1.13.1)

(2)bazel-0.19.2-installer-linux-x86_64.sh(bazel-0.19.2-installer-linux-x86_64.sh)

(3)Anaconda3-4.3.1-Linux-x86_64.sh(Anaconda3-4.3.1-Linux-x86_64.sh)

(4)protobuf-2.6.1.tar.gz(protobuf-2.6.1.tar.gz)

2、不同平台版本对应要求

注意:TensorFlow C++ API的编译对于bazel等工具的对应版本有要求,版本需要匹配,不然会有很多想不到的错误。

(1)Windows-CPU

版本Python 版本编译器编译工具tensorflow-1.13.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.12.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.11.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.10.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.9.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.8.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.7.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.6.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.5.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.4.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.3.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.2.03.5-3.6MSVC 2015 update 3Cmake v3.6.3tensorflow-1.1.03.5MSVC 2015 update 3Cmake v3.6.3tensorflow-1.0.03.5MSVC 2015 update 3Cmake v3.6.3

(2)Linux-CPU

版本Python 版本编译器编译工具tensorflow-1.13.12.7、3.3-3.6GCC 4.8Bazel 0.19.2tensorflow-1.12.02.7、3.3-3.6GCC 4.8Bazel 0.15.0tensorflow-1.11.02.7、3.3-3.6GCC 4.8Bazel 0.15.0tensorflow-1.10.02.7、3.3-3.6GCC 4.8Bazel 0.15.0tensorflow-1.9.02.7、3.3-3.6GCC 4.8Bazel 0.11.0tensorflow-1.8.02.7、3.3-3.6GCC 4.8Bazel 0.10.0tensorflow-1.7.02.7、3.3-3.6GCC 4.8Bazel 0.10.0tensorflow-1.6.02.7、3.3-3.6GCC 4.8Bazel 0.9.0tensorflow-1.5.02.7、3.3-3.6GCC 4.8Bazel 0.8.0tensorflow-1.4.02.7、3.3-3.6GCC 4.8Bazel 0.5.4tensorflow-1.3.02.7、3.3-3.6GCC 4.8Bazel 0.4.5tensorflow-1.2.02.7、3.3-3.6GCC 4.8Bazel 0.4.5tensorflow-1.1.02.7、3.3-3.6GCC 4.8Bazel 0.4.2tensorflow-1.0.02.7、3.3-3.6GCC 4.8Bazel 0.4.2

 (3)MacOS-CPU

版本Python 版本编译器编译工具tensorflow-1.13.12.7、3.3-3.6XCode 中的 ClangBazel 0.19.2tensorflow-1.12.02.7、3.3-3.6XCode 中的 ClangBazel 0.15.0tensorflow-1.11.02.7、3.3-3.6XCode 中的 ClangBazel 0.15.0tensorflow-1.10.02.7、3.3-3.6XCode 中的 ClangBazel 0.15.0tensorflow-1.9.02.7、3.3-3.6XCode 中的 ClangBazel 0.11.0tensorflow-1.8.02.7、3.3-3.6XCode 中的 ClangBazel 0.10.1tensorflow-1.7.02.7、3.3-3.6XCode 中的 ClangBazel 0.10.1tensorflow-1.6.02.7、3.3-3.6XCode 中的 ClangBazel 0.8.1tensorflow-1.5.02.7、3.3-3.6XCode 中的 ClangBazel 0.8.1tensorflow-1.4.02.7、3.3-3.6XCode 中的 ClangBazel 0.5.4tensorflow-1.3.02.7、3.3-3.6XCode 中的 ClangBazel 0.4.5tensorflow-1.2.02.7、3.3-3.6XCode 中的 ClangBazel 0.4.5tensorflow-1.1.02.7、3.3-3.6XCode 中的 ClangBazel 0.4.2tensorflow-1.0.02.7、3.3-3.6XCode 中的 ClangBazel 0.4.2

(4) Windows-GPU

版本Python 版本编译器编译工具cuDNNCUDAtensorflow_gpu-1.13.03.5-3.6MSVC 2015 update 3Bazel 0.15.079tensorflow_gpu-1.12.03.5-3.6MSVC 2015 update 3Bazel 0.15.079tensorflow_gpu-1.11.03.5-3.6MSVC 2015 update 3Bazel 0.15.079tensorflow_gpu-1.10.03.5-3.6MSVC 2015 update 3Cmake v3.6.379tensorflow_gpu-1.9.03.5-3.6MSVC 2015 update 3Cmake v3.6.379tensorflow_gpu-1.8.03.5-3.6MSVC 2015 update 3Cmake v3.6.379tensorflow_gpu-1.7.03.5-3.6MSVC 2015 update 3Cmake v3.6.379tensorflow_gpu-1.6.03.5-3.6MSVC 2015 update 3Cmake v3.6.379tensorflow_gpu-1.5.03.5-3.6MSVC 2015 update 3Cmake v3.6.379tensorflow_gpu-1.4.03.5-3.6MSVC 2015 update 3Cmake v3.6.368tensorflow_gpu-1.3.03.5-3.6MSVC 2015 update 3Cmake v3.6.368tensorflow_gpu-1.2.03.5-3.6MSVC 2015 update 3Cmake v3.6.35.18tensorflow_gpu-1.1.03.5MSVC 2015 update 3Cmake v3.6.35.18tensorflow_gpu-1.0.03.5MSVC 2015 update 3Cmake v3.6.35.18

(5)Linux-GPU

版本Python 版本编译器编译工具cuDNNCUDAtensorflow_gpu-1.13.12.7、3.3-3.6GCC 4.8Bazel 0.19.27.410.0tensorflow_gpu-1.12.02.7、3.3-3.6GCC 4.8Bazel 0.15.079tensorflow_gpu-1.11.02.7、3.3-3.6GCC 4.8Bazel 0.15.079tensorflow_gpu-1.10.02.7、3.3-3.6GCC 4.8Bazel 0.15.079tensorflow_gpu-1.9.02.7、3.3-3.6GCC 4.8Bazel 0.11.079tensorflow_gpu-1.8.02.7、3.3-3.6GCC 4.8Bazel 0.10.079tensorflow_gpu-1.7.02.7、3.3-3.6GCC 4.8Bazel 0.9.079tensorflow_gpu-1.6.02.7、3.3-3.6GCC 4.8Bazel 0.9.079tensorflow_gpu-1.5.02.7、3.3-3.6GCC 4.8Bazel 0.8.079tensorflow_gpu-1.4.02.7、3.3-3.6GCC 4.8Bazel 0.5.468tensorflow_gpu-1.3.02.7、3.3-3.6GCC 4.8Bazel 0.4.568tensorflow_gpu-1.2.02.7、3.3-3.6GCC 4.8Bazel 0.4.55.18tensorflow_gpu-1.1.02.7、3.3-3.6GCC 4.8Bazel 0.4.25.18tensorflow_gpu-1.0.02.7、3.3-3.6GCC 4.8Bazel 0.4.25.18

(6)MacOS-GPU

版本Python 版本编译器编译工具cuDNNCUDAtensorflow_gpu-1.1.02.7、3.3-3.6XCode 中的 ClangBazel 0.4.25.18tensorflow_gpu-1.0.02.7、3.3-3.6XCode 中的 ClangBazel 0.4.25.18 3、环境配置

(1)安装Anaconda

/mnt/f/linux$ ./Anaconda3-4.3.1-Linux-x86_64.sh

安装过程中如果忘记将Anaconda添加环境变量,请使用以下方法添加环境变量。

方法一: 将Anaconda添加环境变量(该方法退出Linux重启后无效)

$ export PATH=/home/asialee/anaconda3/bin:$PATH $ source ~/.bashrc

方法二:将Anaconda永久添加环境变量(该方法退出Linux重启后仍有效,配置稍微麻烦点)

$sudo vim /etc/profile

然后在打开的profile文本最后一行添加:export PATH=~/anaconda3/bin:$PATH 

最后注入环境变量,让其立即生效,重启也有效

source /etc/profile

添加环境变量完成后,输入python后得到显示,即配置完成。

(2)安装部分软件

安装bazel或者以下编译操作可能需要安装的部分软件如下:

$ sudo apt install unzip $ sudo apt install make $ sudo apt install g++ $ sudo apt install gcc $ sudo apt install cmake sudo apt-get install autoconf sudo apt-get install automake sudo apt-get install libtool sudo apt install curl(used to download gmock) sudo apt-get install zlib1g-dev sudo apt-get install liblzma-dev

如果安装不成功,则需要更新sudo后再尝试安装,即:sudo apt update。

(3)安装bazel

$ ./bazel-0.19.2-installer-linux-x86_64.sh --user

(4)安装protobuf

protobuf是最重要的一个,对版本要求非常严格,版本不对应会出现很多问题;本次使用的版本是protobuf 2.6.1,比较稳定。

解压protobuf-2.6.1.tar.gz

/mnt/f/linux$ tar -zxvf protobuf-2.6.1.tar.gz

进入解压后的protobuf-2.6.1文件目录,执行以下命令

/mnt/f/linux/protobuf-2.6.1$ ./configure /mnt/f/linux/protobuf-2.6.1$ make /mnt/f/linux/protobuf-2.6.1$ make install

注意:如果输入make install 时会出现make: *** [install-recursive] Error 1问题,则输入sudo make install 即可解决。

方法一:将protobuf加入环境变量(该方法重启后无效)

$ export LD_LIBRARY_PATH=/usr/local/lib

方法二: 将protobuf永久加入环境变量(重启后仍有效)

$sudo vim /etc/profile然后在打开的profile文本最后一行添加:export LD_LIBRARY_PATH=/usr/local/lib最后注入环境变量,让其立即生效,重启也有效:source /etc/profile

查看版本以验证安装

$ protoc --version 4、配置TensorFlow安装选项

进入源码根目录,运行 ./configure 进行配置。可参考 官网 -> Build from source -> View sample configuration session 设置,如果只需要配置cpu环境就一直回车。

/mnt/f/linux/tensorflow-1.13.1$ ./configure 5、使用bazel进行编译生成动态库

#编译C++ API,生成.so文件,Tensorflow调用CUDA

/mnt/f/linux/tensorflow-1.13.1$ bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so

#编译C++ API,生成.so文件,Tensorflow不调用CUDA

/mnt/f/linux/tensorflow-1.13.1$ bazel build --config=opt //tensorflow:libtensorflow_cc.so

等待编译大约半个多小时,就成功编译:

编译成功后,tensorflow-1.13.1目录下会出现 bazel-xxx 的几个文件,在tensorflow-1.13.1/bazel-bin/tensorflow文件下会出现 libtensorflow_cc.so 和 libtensorflow_framework.so 动态库文件。(动态库文件下载)

6、编译其他依赖 cd /mnt/f/linux/tensorflow-1.13.1/tensorflow/contrib/makefile$ ./build_all_linux.sh

执行成功后,在tensorflow-1.13.1/tensorflow/contrib/makefile目录下:downloads文件夹下存放第三方依赖的一些头文件和静态库,比如nsync、Eigen、protobuf等。

说明:Eigen 是一个高层次的C ++库,有效支持线性代数,矩阵和矢量运算,数值分析及其相关的算法;Protocol Buffers (简称 Protobuf)是 Google 开源的一款跨语言,跨平台,扩展性好的序列化工具,相比于 XML 和 JSON 等流行的编码格式,这种数据结构化语言需要使用protoc进行编译。

7、测试一:简单测试编译好的TensorFlow C++ API

(1)创建一个demo文件夹,demo文件夹下具体内容目录如下

├── src | └── test.cpp ├── CMakeLists.txt

其中,test.cpp为C++测试代码,具体如下:

#include #include #include using namespace std; using namespace tensorflow; int main() { Session* session; Status status = NewSession(SessionOptions(), &session); if (!status.ok()) { cout Run( {{ graph_def.saver_def().filename_tensor_name(), checkpointPathTensor },}, {}, {graph_def.saver_def().restore_op_name()}, nullptr); if (!status.ok()) { throw runtime_error("Error loading checkpoint from " + checkpointPath + ": " + status.ToString()); } // 构造模型的输入,相当与python版本中的feed std::vector input; tensorflow::TensorShape inputshape; inputshape.InsertDim(0,1); Tensor a(tensorflow::DT_INT32,inputshape); Tensor b(tensorflow::DT_INT32,inputshape); auto a_map = a.tensor(); a_map(0) = 2; auto b_map = b.tensor(); b_map(0) = 3; input.emplace_back(std::string("a"), a); input.emplace_back(std::string("b"), b); // 运行模型,并获取输出 std::vector answer; status = session->Run(input, {"res"}, {}, &answer); Tensor result = answer[0]; auto result_map = result.tensor(); cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有